package com.wlyuan.open;

import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import lombok.var;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.stream.Collectors;

public class ResourceFileReader {
    @SneakyThrows
    public InputStream read(String fileName) {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        return classPathResource.getInputStream();
    }

    @SneakyThrows
    public List<String> readLines(String fileName) {
        var inputStream = read(fileName);
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        return br.lines().collect(Collectors.toList());
    }

    public <T> T readObject(String fileName, Class<T> clazz) {
        List<String> lines = readLines(fileName);
        String readString = CollectionUtils.join(lines, "");
        return JSON.parseObject(readString, clazz);
    }
}
